﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using ICSharpCode.Core.Presentation;
using ICSharpCode.Core;

namespace MyWPFWorkBench.FluentRibbon
{
    
    public class RibbonGroupBox : Fluent.RibbonGroupBox, IStatusUpdate
    {
        protected readonly Codon codon;
        protected readonly object caller;
        protected readonly IEnumerable<ICondition> conditions;

        /// <summary>
        /// If true, UpdateStatus() sets the enabled flag.
        /// Used for type=Menu, but not for type=MenuItem - for menu items, Enabled is controlled by the WPF ICommand.
        /// </summary>
        internal bool SetEnabled;

        public RibbonGroupBox(Codon codon, object caller, IEnumerable<ICondition> conditions)
        {
            this.codon = codon;
            this.caller = caller;
            this.conditions = conditions;

            UpdateText();
        }

        public void UpdateText()
        {
            if (codon != null)
            {
                Header = MenuService.ConvertLabel(StringParser.Parse(codon.Properties["label"]));
            }
        }

        public virtual void UpdateStatus()
        {
            ConditionFailedAction result = ICSharpCode.Core.Condition.GetFailedAction(conditions, caller);
            if (result == ConditionFailedAction.Exclude)
                this.Visibility = Visibility.Collapsed;
            else
                this.Visibility = Visibility.Visible;
            if (SetEnabled)
                this.IsEnabled = result == ConditionFailedAction.Nothing;
        }
    }
}
